package com.ruoyi.web.controller.tcdx;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.FlowableActRuTask;
import com.ruoyi.system.flowable.service.IFlowableService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.service.RequestParameter;

import java.util.List;
import java.util.Map;

/**
 * 通用请求处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/flowable")
public class FLowableMonitorController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(FLowableMonitorController.class);
    private String prefix = "";


    @RequiresPermissions("system:flowable:monitor")
    @GetMapping("/monitor")
    public String getFlowableMoitorPage()
    {
        return  prefix + "flowable_monitor";
    }

    @Autowired
    private IFlowableService flowableService;
    /**
     * 查询参数配置列表
     */
    @RequiresPermissions("system:flowable:get_act_ru_task_pagedata")
    @PostMapping("/get_act_ru_task_pagedata")
    @ResponseBody
    public TableDataInfo get_act_ru_task_pagedata(FlowableActRuTask task)
    {
        startPage();
        List<Map> list = flowableService.selectFlowableActRuTaskList(task);
        return getDataTable(list);
    }

    @RequiresPermissions("system:flowable:get_act_re_procdef_pagedata")
    @PostMapping("/get_act_re_procdef_pagedata")
    @ResponseBody
    public TableDataInfo get_act_re_procdef_pagedata()
    {
        startPage();
        List<Map> list = flowableService.selectFlowableActReProcDefList();
        return getDataTable(list);
    }

    @RequiresPermissions("system:flowable:get_act_re_deployment_pagedata")
    @PostMapping("/get_act_re_deployment_pagedata")
    @ResponseBody
    public TableDataInfo get_act_re_deployment_pagedata()
    {
        startPage();
        List<Map> list = flowableService.select_flowable_act_re_deployment_pagedata();
        return getDataTable(list);
    }

    @RequiresPermissions("system:flowable:proc_inst_task_complete")
    @PostMapping("/proc_inst_task_complete")
    @ResponseBody
    public AjaxResult proc_inst_task_complete(String proc_inst_id)
    {
        flowableService.proc_inst_task_complete(proc_inst_id);
        return AjaxResult.success("成功");
    }

    @RequiresPermissions("system:flowable:proc_inst_task_start")
    @PostMapping("/proc_inst_task_start")
    @ResponseBody
    public AjaxResult proc_inst_task_start(String flow_def_id)
    {

        flowableService.proc_inst_task_start(flow_def_id);
        return AjaxResult.success("1");
    }

    @RequiresPermissions("system:flowable:delete_deploy")
    @PostMapping("/delete_deploy")
    @ResponseBody
    public AjaxResult delete_deploy(String flow_def_id)
    {

        flowableService.delete_deploy(flow_def_id);
        return AjaxResult.success("1");
    }

    @RequiresPermissions("system:flowable:deploy_flow")
    @PostMapping("/deploy_flow")
    @ResponseBody
    public AjaxResult deploy_flow(@RequestParam Map<String, Object> map)
    {
        System.out.println("接受到的模型：");
        System.out.println(map);
        String flow_def_id=flowableService.deploy_flow(map.get("flow_def_key").toString(),Math.random()+".bpmn20.xml",map.get("flow_text").toString());
        return AjaxResult.success(flow_def_id);
    }

    /**
     * 根据process_def_id获取流程定义的XML
     *
     * **/
    @RequiresPermissions("system:flowable:get_deploy_flow_by_id")
    @PostMapping("/get_deploy_flow_by_id")
    @ResponseBody
    public AjaxResult get_deploy_flow_by_id(@RequestParam Map<String, Object> map)
    {
        System.out.println("接受到的模型：");
        System.out.println(map);
        String  deloy_flow= flowableService.get_deploy_flow_by_id(map.get("flow_def_id").toString());
        return AjaxResult.success(deloy_flow);
    }


}
